<?
class db_init{
    var $sever;
    var $username;
    var $password;
    var $name_database;
    function db_init(){
        $this->sever            = 'localhost';
        $this->username         = 'root';
        $this->password         = '';
        $this->database         = 'db_manhvuong';
    }
    function log(){
        $conn = mysql_connect($this->sever,$this->username,$this->password);
        mysql_select_db($this->database);
    }
}

class db_query{
	/** Ket qua cua cau truy van */
	var $result;
    /** ket noi */
	var $links;
    /** Thoi gian nhieu nhat 1 cau query duoc thuc hien */
	var $time_slow_log = 0.05;     //

    /*********************************************************************************************************/
	/**
	 * db_query::db_query()
	 *
	 * @param mixed $query : cau truy van
	 * @param string $file_line_query : loi se ghi
	 * @return
	 */
	function db_query($query, $file_line_query = ""){
		$dbinit       = new db_init();
		//Khai bao connect
		$this->links  = @mysql_connect($dbinit->sever, $dbinit->username, $dbinit->password)or die('Không thể kết nối');

		$db_select    = mysql_select_db($dbinit->database,$this->links);
		@mysql_query("SET NAMES 'utf8'");
		$this->result = @mysql_query($query, $this->links);
		unset($dbinit);
	}

    /*********************************************************************************************************/
	/**
	 * db_query::resultArray()
	 * Ham lay ket qua
	 * @return array $arrayReturn : Mang
	 */
	function resultArray($field_id = ""){
		$arrayReturn = array();
		while($row = mysql_fetch_assoc($this->result)){
			if($field_id != ""){
				$arrayReturn[$row[$field_id]] = $row;
			}else{
				$arrayReturn[] = $row;
			}

		}
		return $arrayReturn;
	}

    /*********************************************************************************************************/
	/**
	 * db_query::close()
	 * Ham dong ket noi
	 * @return
	 */
	function close(){
		@mysql_free_result($this->result);
		if ($this->links){
			@mysql_close($this->links);
		}
	}

    /*********************************************************************************************************/
	/**
	 * db_query::microtime_float()
	 * Ham tinh thoi gian(miligiay)
	 * @return float $return
	 */
	function microtime_float(){
	 list($usec, $sec) = explode(" ", microtime());
	 return ((float)$usec + (float)$sec);
	}
}

/**
 * Class db_execute
 * Class thuc thi 1 query
 */
class db_execute{
    /** ket noi */
	var $links;
    /** so dong bi anh huong */
	var $total = 0;

    /*********************************************************************************************************/
	/**
	 * db_execute::db_execute()
	 * Thuc hien 1 cau query
	 * @param mixed $query : Cau query
	 * @param string $file_line_query : Ghi loi
	 * @return
	 */
	function db_execute($query, $file_line_query = ""){
		$dbinit       = new db_init();
        $this->links  = @mysql_connect($dbinit->sever, $dbinit->username, $dbinit->password);
		@mysql_select_db($dbinit->database);
		@mysql_query("SET NAMES 'utf8'");
		@mysql_query($query);

		//kiem tra thanh cong hay chua
		$this->total = @mysql_affected_rows();
		@mysql_close($this->links);
		unset($dbinit);
	}
}
?>